import { ref } from 'vue';

export function useDeleteConfirmation() {
  const isDeleting = ref(false);

  const deleteData = (selectData, doDelete) => {
    // isDeleting状态用于表示是否正在执行删除操作
    if (isDeleting.value) return;

    if (!selectData.length) {
      ElNotification({
        title: 'Warning',
        message: '请选择数据，再删除！',
        type: 'warning',
      });
      return;
    }

    ElMessageBox.confirm('此操作将删除数据，是否继续?', 'Warning', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
      .then(() => {
        isDeleting.value = true; // 设置正在删除的状态
        console.log('selectData', selectData);
        if (doDelete && typeof doDelete === 'function') {
          // 调用传入的doDelete函数进行删除
          doDelete(selectData).finally(() => {
            isDeleting.value = false; // 清除正在删除的状态
          });
        }
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消删除',
        });
      });
  };

  // 返回hook的API：deleteData函数和isDeleting状态
  return { deleteData, isDeleting };
}
